﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Linq;
using System.Data;
using System.Xml.Linq;
using System.Collections.Generic;
using EntLibContrib.Query.Commands;

namespace EntLibContrib.Query.QuickStart.CustomCommands
{
    using Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;

    /// <summary>
	/// Custom file command to delete a Basic3 object
	/// </summary>
    [ConfigurationElementType(typeof(DeleteBasic3ObjectCommandData))]
    public class DeleteBasic3ObjectCommand : FileCommand
	{
		#region Constants
		private const string objectElement = "Basic3";
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="DeleteBasic3ObjectCommand"/> class.
		/// </summary>
		public DeleteBasic3ObjectCommand()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Executes a read action.
		/// </summary>
		/// <returns>
		/// a DataSet containing the results of the read action
		/// </returns>
		public override DataSet ExecuteForRead()
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Executes a write action.
		/// </summary>
		public override void ExecuteForWrite()
		{
			// read the xml document
			XDocument document = XDocument.Load(DataFile, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
			// search for an existing Basic3 object with the same Id
			IEnumerable<XElement> elements = document.Descendants(objectElement).Where(a => ((Guid)a.Attribute("Id")) == (Guid)Parameters["Id"].Value);
			// if the Id was found then delete the object
			if (elements.Count<XElement>() > 0)
			{
				foreach (XElement element in elements)
				{
					// check for permanent objects
					if (!(bool)element.Element("IsPermanent"))
					{
						// delete the basic3 object
						element.SetElementValue("DeletedIdentityId", (Guid)Parameters["DeletedIdentityId"].Value);
						element.SetElementValue("DeletedDate", (DateTime)Parameters["DeletedDate"].Value);
					}
					break; // there should only be one
				}
				// save the xml document
				document.Save(DataFile);
			}
		}
		#endregion
	}

    public class DeleteBasic3ObjectCommandData : CommandData
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DeleteBasic3ObjectCommandData"/> class.
        /// </summary>
        public DeleteBasic3ObjectCommandData()
            : base(typeof(DeleteBasic3ObjectCommand))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DeleteBasic3ObjectCommandData"/> class.
        /// </summary>
        public DeleteBasic3ObjectCommandData(string name)
            : base(name, typeof(DeleteBasic3ObjectCommand))
        {
        }

        public override Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new DeleteBasic3ObjectCommand())
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}
